home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / getcwd.c < prev    next >
C/C++ Source or Header  |  1994-05-19  |  6KB  |  249 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <hurd.h>
  23. #include <hurd/port.h>
  24. #include <dirent.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30.  
  31.  
  32. /* Get the pathname of the current working directory, and put it in SIZE
  33.    bytes of BUF.  Returns NULL if the directory couldn't be determined or
  34.    SIZE was too small.  If successful, returns BUF.  In GNU, if BUF is
  35.    NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
  36.    unless SIZE <= 0, in which case it is as big as necessary.  */
  37.  
  38. char *
  39. getcwd (char *buf, size_t size)
  40. {
  41.   error_t err;
  42.   dev_t rootdev, thisdev;
  43.   ino_t rootino, thisino;
  44.   char *path;
  45.   register char *pathp;
  46.   struct stat st;
  47.   file_t parent;
  48.   char *dirbuf = NULL;
  49.   unsigned int dirbufsize = 0;
  50.   file_t crdir;
  51.   struct hurd_userlink crdir_ulink;
  52.  
  53.   inline void cleanup (void)
  54.     {
  55.       _hurd_port_free (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink, crdir);
  56.       __mach_port_deallocate (__mach_task_self (), parent);
  57.  
  58.       if (dirbuf != NULL)
  59.     __vm_deallocate (__mach_task_self (),
  60.              (vm_address_t) dirbuf, dirbufsize);
  61.     }
  62.  
  63.       
  64.   if (size == 0)
  65.     {
  66.       if (buf != NULL)
  67.     {
  68.       errno = EINVAL;
  69.       return NULL;
  70.     }
  71.  
  72.       size = FILENAME_MAX * 4 + 1;    /* Good starting guess.  */
  73.     }
  74.  
  75.   if (buf != NULL)
  76.     path = buf;
  77.   else
  78.     {
  79.       path = malloc (size);
  80.       if (path == NULL)
  81.     return NULL;
  82.     }
  83.  
  84.   pathp = path + size;
  85.   *--pathp = '\0';
  86.  
  87.   /* Get a port to our root directory and stat it.  */
  88.  
  89.   crdir = _hurd_port_get (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink);
  90.   if (err = __io_stat (crdir, &st))
  91.     {
  92.       _hurd_port_free (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink, crdir);
  93.       return __hurd_fail (err), NULL;
  94.     }
  95.   rootdev = st.st_dev;
  96.   rootino = st.st_ino;
  97.  
  98.   /* Get a port to our current working directory and stat it.  */
  99.  
  100.   if (err = __USEPORT (CWDIR, __mach_port_mod_refs (__mach_task_self (),
  101.                             (parent = port),
  102.                             MACH_PORT_RIGHT_SEND,
  103.                             1)))
  104.     {
  105.       _hurd_port_free (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink, crdir);
  106.       return __hurd_fail (err), NULL;
  107.     }
  108.   if (err = __io_stat (parent, &st))
  109.     {
  110.       cleanup ();
  111.       return __hurd_fail (err), NULL;
  112.     }
  113.  
  114.   thisdev = st.st_dev;
  115.   thisino = st.st_ino;
  116.  
  117.   while (!(thisdev == rootdev && thisino == rootino))
  118.     {
  119.       /* PARENT is a port to the directory we are currently on;
  120.      THISDEV and THISINO are its device and node numbers.
  121.      Look in its parent (..) for a file with the same numbers.  */
  122.  
  123.       struct dirent *d;
  124.       dev_t dotdev;
  125.       ino_t dotino;
  126.       int mount_point;
  127.       file_t newp;
  128.       char *dirdata;
  129.       unsigned int dirdatasize;
  130.       off_t dirpos;
  131.  
  132.       /* Look at the parent directory.  */
  133.       if (err = __hurd_path_lookup (crdir, parent, "..", O_READ, 0, &newp))
  134.     goto lose;
  135.       __mach_port_deallocate (__mach_task_self (), parent);
  136.       parent = newp;
  137.  
  138.       /* Figure out if this directory is a mount point.  */
  139.       if (err = __io_stat (parent, &st))
  140.     goto lose;
  141.       dotdev = st.st_dev;
  142.       dotino = st.st_ino;
  143.       mount_point = dotdev != thisdev;
  144.  
  145.       /* Search for the last directory.  */
  146.       dirpos = 0;
  147.       dirdata = dirbuf;
  148.       dirdatasize = dirbufsize;
  149.       while (!(err = __dir_readdir (parent, &dirdata, &dirdatasize,
  150.                     dirpos, &dirpos, st.st_blksize)) &&
  151.          dirdatasize != 0)         
  152.     {
  153.       /* We have a block of directory entries.  */
  154.  
  155.       unsigned int offset;
  156.  
  157.       if (dirdata != dirbuf)
  158.         {
  159.           /* The data was passed out of line, so our old buffer is no
  160.          longer useful.  Deallocate the old buffer and reset our
  161.          information for the new buffer.  */
  162.           __vm_deallocate (__mach_task_self (),
  163.                    (vm_address_t) dirbuf, dirbufsize);
  164.           dirbuf = dirdata;
  165.           dirbufsize = dirdatasize;
  166.         }
  167.  
  168.       /* Iterate over the returned directory entries, looking for one
  169.          whose file number is THISINO.  */
  170.  
  171.       offset = 0;
  172.       while (offset < dirdatasize)
  173.         {
  174.           d = (struct dirent *) &dirdata[offset];
  175.           offset += d->d_reclen;
  176.  
  177.           /* Ignore `.' and `..'.  */    
  178.           if (d->d_name[0] == '.' &&
  179.           (d->d_namlen == 1 ||
  180.            (d->d_namlen == 2 && d->d_name[1] == '.')))
  181.         continue;
  182.  
  183.           if (mount_point || d->d_ino == thisino)
  184.         {
  185.           file_t try;
  186.           if (err = __hurd_path_lookup (crdir, parent, d->d_name,
  187.                         O_NOLINK, 0, &try))
  188.             goto lose;
  189.           err = __io_stat (try, &st);
  190.           __mach_port_deallocate (__mach_task_self (), try);
  191.           if (err)
  192.             goto lose;
  193.           if (st.st_dev == thisdev && st.st_ino == thisino)
  194.             break;
  195.         }
  196.         }
  197.     }
  198.  
  199.       if (err)
  200.     goto lose;
  201.       else
  202.     {
  203.       /* Prepend the directory name just discovered.  */
  204.  
  205.       if (pathp - path < d->d_namlen + 1)
  206.         {
  207.           if (buf != NULL)
  208.         {
  209.           errno = ERANGE;
  210.           return NULL;
  211.         }
  212.           else
  213.         {
  214.           size *= 2;
  215.           buf = realloc (path, size);
  216.           if (buf == NULL)
  217.             {
  218.               free (path);
  219.               return NULL;
  220.             }
  221.           pathp = &buf[pathp - path];
  222.           path = buf;
  223.         }
  224.         }
  225.       pathp -= d->d_namlen;
  226.       (void) memcpy (pathp, d->d_name, d->d_namlen);
  227.       *--pathp = '/';
  228.     }
  229.  
  230.       /* The next iteration will find the name of the directory we
  231.      just searched through.  */
  232.       thisdev = dotdev;
  233.       thisino = dotino;
  234.     }
  235.  
  236.   if (pathp == &path[size - 1])
  237.     /* We found nothing and got all the way to the root.
  238.        So the root is our current directory.  */
  239.     *--pathp = '/';
  240.  
  241.   memmove (path, pathp, path + size - pathp);
  242.   cleanup ();
  243.   return path;
  244.  
  245.  lose:
  246.   cleanup ();
  247.   return NULL;
  248. }
  249.